//
//  SNActivitiesListCell.m
//  app
//
//  Created by 王少宁 on 15/8/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNActivitiesListCell.h"

@interface SNActivitiesListCell ()
@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, strong) UIView * itemZero;
@property (nonatomic, strong) UIView * itemOne;
@property (nonatomic, strong) UIView * itemTwo;
@end

@implementation SNActivitiesListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellHeight:(CGFloat)cellHeight
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _cellHeight = cellHeight;
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    CGFloat itemW = (SCREEN_WIDTH - UP(2)) / 3;
    CGFloat itemH = _cellHeight - UP(1);
    
    for (int i = 0; i < 3; i++) {
        
        UIView * subItemBackView = [[UIView alloc] initWithFrame:CGRectMake(i * itemW+UP(i), 0, itemW, itemH)];
        subItemBackView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:subItemBackView];
        
        UIImageView * subItemPicView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, itemW, itemH - 25)];
        subItemPicView.contentMode = UIViewContentModeScaleAspectFit;
        [subItemBackView addSubview:subItemPicView];
        
        UILabel * subItemTitleLabel = CREATE_WITH_FRAME_OF(UILabel, CGPointMake(0, itemH - 15),
                                                           [subItemPicView.ubo fw:1.0 oh:12]);
        subItemTitleLabel.font = JKAppGlobleFontMinTagText;
        subItemTitleLabel.textAlignment = NSTextAlignmentCenter;
        subItemTitleLabel.textColor = [XCommon hexStringToColor:kColor_gray_text];
        [subItemBackView addSubview:subItemTitleLabel];
        
        UIButton * subItemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        subItemBtn.frame = subItemBackView.bounds;
        subItemBtn.tag = i + 1;
        [subItemBtn addTarget:self action:@selector(subItemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [subItemBackView addSubview:subItemBtn];
    
        switch (i) {
            case 0:
                _itemZero = subItemBackView;
                break;
                
            case 1:
                _itemOne = subItemBackView;
                break;
                
            default:
                _itemTwo = subItemBackView;
                break;
        }
    }
    
}

- (void)setShopArray:(NSArray *)shopArray
{
    if (shopArray[0]) {
        _itemZero.userInteractionEnabled = YES;
        SNShopActivitiesListSubModel * subModel = shopArray[0];
        for (UIView * imgView in _itemZero.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:[NSURL URLWithString:subModel.pic]];
            }
        }
        for (UIView * titleLabel in _itemZero.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = subModel.name;
            }
        }
    } else {
        _itemZero.userInteractionEnabled = NO;
        for (UIView * imgView in _itemZero.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:nil];
            }
        }
        for (UIView * titleLabel in _itemZero.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = nil;
            }
        }
    }
    
    
    
    
    
    if (shopArray.count > 1) {
        _itemOne.userInteractionEnabled = YES;
        SNShopActivitiesListSubModel * subModel = shopArray[1];
        for (UIView * imgView in _itemOne.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:[NSURL URLWithString:subModel.pic]];
            }
            
        }
        for (UIView * titleLabel in _itemOne.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = subModel.name;
            }
        }
    } else {
        _itemOne.userInteractionEnabled = NO;
        for (UIView * imgView in _itemOne.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:nil];
            }
            
        }
        for (UIView * titleLabel in _itemOne.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = nil;
            }
        }
    }
    
    

    
    if (shopArray.count > 2) {
        _itemTwo.userInteractionEnabled = YES;
        SNShopActivitiesListSubModel * subModel = shopArray[2];
        for (UIView * imgView in _itemTwo.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:[NSURL URLWithString:subModel.pic]];
            }
            
        }
        for (UIView * titleLabel in _itemTwo.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = subModel.name;
            }
        }
    } else {
        _itemTwo.userInteractionEnabled = NO;
        for (UIView * imgView in _itemTwo.subviews) {
            if ([imgView isKindOfClass:[UIImageView class]]) {
                [(UIImageView *)imgView sd_setImageWithURL:nil];
            }
            
        }
        for (UIView * titleLabel in _itemTwo.subviews) {
            if ([titleLabel isKindOfClass:[UILabel class]]) {
                ((UILabel *)titleLabel).text = nil;
            }
        }
    }

    _shopArray = shopArray;
}

- (void)subItemBtnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(itemClickWithShopBaseModel:row:index:)]) {
        [self.delegate itemClickWithShopBaseModel:self.shopBaseModel row:_row index:btn.tag-1];
    }
}

- (void)drawRect:(CGRect)rect {
    [self drawBackground:rect color:[XCommon hexStringToColor:@"#D3D3D3"]];
}

@end
